package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.dto.AdComment;
import com.heima.feign.ApArticleFeignClient;
import com.heima.feign.CommentFeignClient;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.comment.dtos.CommentPageQueryDTO;
import com.heima.model.review.CommentRepaySaveDto;
import com.heima.model.wemedia.dto.ArticleCommentManagementVO;
import com.heima.model.wemedia.dto.ArticleCommentStatusDto;
import com.heima.model.wemedia.dto.CommentManagementQueryDTO;
import com.heima.wemedia.entity.WmNews;
import com.heima.wemedia.service.WmCommentService;
import com.heima.wemedia.service.WmNewsService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WmCommentServiceImpl implements WmCommentService {

    @Autowired
    private CommentFeignClient client;

    @Autowired
    private ApArticleFeignClient apArticleFeignClient;

    @Autowired
    private WmNewsService wmNewsService;

    @Override
    public void delComment(Long commentId) {
        //todo 感觉需要判断当前用户是否有权限操作当前文章的评论
        client.delComment(commentId);
    }

    @Override
    public void delCommentRepay(Long replyId) {
        client.delCommentRepay(replyId);
    }

    @Override
    public PageResult<ArticleCommentManagementVO> findNewsComments(CommentManagementQueryDTO dto) {
        dto.setWmUserId(UserThreadLocalUtils.getUserId());
        List<ArticleCommentManagementVO> newsComments = apArticleFeignClient.findNewsComments(dto);
        return new PageResult(dto.getPage(), dto.getSize(), Long.valueOf(newsComments.size()), newsComments);
    }

    @Override
    public Object getPage(CommentPageQueryDTO dto) {
        return client.manageList(dto);
    }

    @Override
    public ResponseResult commentRepay(CommentRepaySaveDto dto) {
        return client.commentRepay(dto);
    }

    @Override
    public void updateCommentStatus(ArticleCommentStatusDto dto) {
        //判断当前用户是否是文章作者，有权限操作该文章
        Integer userId = UserThreadLocalUtils.getUserId();
        //根据文章id查询自媒体文章id
        Integer wmNewsId = apArticleFeignClient.findWmNewsIdByArticleId(dto.getArticleId());
        //根据userid和自媒体文章id查询自媒体文章
        QueryWrapper<WmNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmNews::getId, wmNewsId);
        queryWrapper.lambda().eq(WmNews::getWmUserId, userId);
        WmNews wmNews = wmNewsService.getOne(queryWrapper);

        if (wmNews == null) {
            //当前用户没有操作权限
            throw new LeadException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }


        if (dto.getOperation() == ArticleCommentStatusDto.operation.CLOSE.getCode()) {
            //关闭，更新article文章
            updateArticleIsComment(dto.getArticleId(), false);
            //删除所有评论
            client.delCommentByArtilceId(dto.getArticleId());
        } else if (dto.getOperation() == ArticleCommentStatusDto.operation.OPEN.getCode()) {
            //开启，
            updateArticleIsComment(dto.getArticleId(), true);
        }
    }

    public void updateArticleIsComment(Long articleId, Boolean isComment) {
        ArticleDto articleDto = new ArticleDto();
        articleDto.setId(articleId);
        articleDto.setIsComment(isComment);
        apArticleFeignClient.updateArticleEnableComment(articleDto);
    }
}
